$(document).ready(function () {
    //顶部菜单
    const topMenuLiEle = $('.module-menu-list>ul>li'),
        leftMenuDivEle = $('aside.main-sidebar>section>div');
    topMenuLiEle.click(function () {
        topMenuLiEle.removeClass('active');$(this).addClass('active');
        leftMenuDivEle.addClass('hide');
        $('#'+$(this).attr('data-id')).removeClass('hide');
    });
    var storage = window.localStorage;
    leftMenuDivEle.click(function () {
        storage.setItem('menuIndex',leftMenuDivEle.index(this));
    });
    if(!topMenuLiEle.parent('ul').find('.active').length){
        var menuIndex = storage.getItem('menuIndex')||0;
        var indexLeftMenuDiv = topMenuLiEle.eq(menuIndex).addClass('active').attr('data-id');
        $('#'+indexLeftMenuDiv).removeClass('hide');
    }


    //弹窗加载
    $('a.layer-open').click(function () {
        var url = $(this).attr('layer-href') || $(this).attr('href');
        if(url.indexOf("?")>=0){
            url+='&iframe=1';
        }else{
            url+='?iframe=1';
        }
        layer.open({
            type: 2,
            title: $(this).attr('layer-title') || $(this).text(),
            area:[$(this).attr('layer-width') || '1080px', $(this).attr('layer-height') || '760px'],
            content: url,
        });
        return false;
    });

    //layer 自定义配置
    layer.config({
        extend: 'easydo/style.css', //加载您的扩展样式
        skin: 'layer-ext-easydo'
    });
    //扩展jQuery方法
    $.extend({
        refreshWindows : function(refresh=1){
            layer.closeAll();
            refresh && location.reload();
        }
    });
    //修改yii confirm方法
    yii.confirm =  function (message, ok, cancel) {
        //$(this) a 节点
        layer.confirm(message, {
            icon: 3,
            offset: '100px',
            title:'提示',
            resize :false,
            btn: ['确定','取消'],
        }, function(){
            !ok || ok();
        }, function(){
            !cancel || cancel();
        });
    };

    //切换主题
    $('#change-theme').click(function () {
        layer.open({
            title:'选择主题',
            btn:[],
            content: '<ul class="themes list-unstyled clearfix"><li><a href="javascript:void(0)"data-skin="skin-blue"class="clearfix full-opacity-hover"><div><span style="background: #367fa9"></span><span class="bg-light-blue"></span></div><div><span style="background: #222d32"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin">蓝色</p></li><li><a href="javascript:void(0)"data-skin="skin-black"class="clearfix full-opacity-hover"><div style="box-shadow: 0 0 2px rgba(0,0,0,0.1)"class="clearfix"><span style="background: #fefefe"></span><span style="background: #fefefe"></span></div><div><span style="background: #222"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin">白色</p></li><li><a href="javascript:void(0)"data-skin="skin-purple"class="clearfix full-opacity-hover"><div><span class="bg-purple-active"></span><span class="bg-purple"></span></div><div><span style="background: #222d32"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin">紫色</p></li><li><a href="javascript:void(0)"data-skin="skin-green"class="clearfix full-opacity-hover"><div><span class="bg-green-active"></span><span class="bg-green"></span></div><div><span style="background: #222d32"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin">绿色</p></li><li><a href="javascript:void(0)"data-skin="skin-red"class="clearfix full-opacity-hover"><div><span class="bg-red-active"></span><span class="bg-red"></span></div><div><span style="background: #222d32"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin">红白</p></li><li><a href="javascript:void(0)"data-skin="skin-yellow"class="clearfix full-opacity-hover"><div><span class="bg-yellow-active"></span><span class="bg-yellow"></span></div><div><span style="background: #222d32"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin">黄色</p></li><li><a href="javascript:void(0)"data-skin="skin-blue-light"class="clearfix full-opacity-hover"><div><span style="background: #367fa9"></span><span class="bg-light-blue"></span></div><div><span style="background: #f9fafc"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin"style="font-size: 12px">蓝白</p></li><li><a href="javascript:void(0)"data-skin="skin-black-light"class="clearfix full-opacity-hover"><div style="box-shadow: 0 0 2px rgba(0,0,0,0.1)"class="clearfix"><span style="background: #fefefe"></span><span style="background: #fefefe"></span></div><div><span style="background: #f9fafc"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin"style="font-size: 12px">双白</p></li><li><a href="javascript:void(0)"data-skin="skin-purple-light"class="clearfix full-opacity-hover"><div><span class="bg-purple-active"></span><span class="bg-purple"></span></div><div><span style="background: #f9fafc"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin"style="font-size: 12px">紫白</p></li><li><a href="javascript:void(0)"data-skin="skin-green-light"class="clearfix full-opacity-hover"><div><span class="bg-green-active"></span><span class="bg-green"></span></div><div><span style="background: #f9fafc"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin"style="font-size: 12px">绿白</p></li><li><a href="javascript:void(0)"data-skin="skin-red-light"class="clearfix full-opacity-hover"><div><span class="bg-red-active"></span><span class="bg-red"></span></div><div><span style="background: #f9fafc"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin"style="font-size: 12px">红白</p></li><li><a href="javascript:void(0)"data-skin="skin-yellow-light"class="clearfix full-opacity-hover"><div><span class="bg-yellow-active"></span><span class="bg-yellow"></span></div><div><span style="background: #f9fafc"></span><span style="background: #f4f5f7"></span></div></a><p class="text-center no-margin"style="font-size: 12px">黄白</p></li></ul>'
        });
    });
    $(document).on('click','.list-unstyled a[data-skin]',function () {
        var theme = $(this).attr('data-skin');
        $.post('/site/theme',{theme:theme},function (res) {
            if(res.code){
                $('body').removeClass(function(index, classes) {
                    var reg = /^skin-/;
                    return classes.split(/\s+/).filter(function(c) {
                        return reg.test(c);
                    }).join(' ');
                }).addClass(theme);
            }
            layer.msg(res.message||'修改主题失败');
        });
    });

    //修改密码
    $('#change-password').click(function (e) {
        e.preventDefault();
        var self = $(this);
        layer.prompt({title: '请输入新密码(6-20位):'}, function(text, index){
            var length = text.length;
            if(length<6 || length>20){
                layer.msg('密码长度为6-20个字符!');
            }else {
                $.post(self.attr('href'),{password:text},function (res) {
                    if(res.code){
                        layer.close(index);
                    }
                    layer.msg(res.message);
                });
            }
        });
    });

    //重置搜索表单
    $('form#form-search button[type="reset"]').click(function () {
        if(location.search){
            location.href = location.origin+location.pathname;
        }
    });
    //自动关闭session提示信息
    setTimeout(function () {
        $(".content").find('div.alert button[class="close"][type="button"]').click();
    },5000);

    //layer预览图片列表
    layer.photos({
        photos: '.file-div-box,.media-select-items,table.table-list>tbody'
        ,shade: 0.2
    });
    //ajax全局设置
    $.ajaxSetup({
        beforeSend : function(jqXHR){
            jqXHR.layerLoadIndex = layer.load(1,{shade:0.3,time: 5000});
        },
        complete : function(jqXHR, textStatus){
            layer.close(jqXHR.layerLoadIndex);
        },
        error : function(jqXHR, textStatus, errorThrown) {
            layer.close(jqXHR.layerLoadIndex);
            var layerConfig = {icon : 2,btn:[]};
            switch (jqXHR.status) {
                case (400):
                    layer.alert(jqXHR.responseText, layerConfig);
                    break;
                case (401):
                    layer.alert('未登录', layerConfig);
                    break;
                case (403):
                    layer.alert('无权限执行此操作', layerConfig);
                    break;
                case (408):
                    layer.alert('请求超时', layerConfig);
                    break;
                case (500):
                    layer.alert('服务器系统内部错误', layerConfig);
                    break;
                default:
                    layer.alert('未知错误,请联系管理员', layerConfig);
            }
        },
        cache : false
    });
});
//yii 提交表单加载loading
(function(yii) {
    function showLoading() {
        var $self = $(this),
            data = $self.data('yiiActiveForm'),
            $submit = data.submitObject;
        if (!$submit) {
            $submit = $self.find('[type=submit]');
        }
        $submit.button('loading');
    }

    function resetLoading() {
        var $self = $(this),
            _data = $self.data('yiiActiveForm'),
            $submit = _data.submitObject;

        if (!_data.submitting) {
            return null;
        }
        if (!$submit) {
            $submit = $self.find('[type=submit]');
        }
        $submit.button('reset');
    }

    yii.lock = {
        init: function() {
            $('body')
                .on('beforeSubmit', 'form', showLoading)
                .on('ajaxComplete', 'form', resetLoading);
        }
    };
})(yii);